using System.Collections;
using System.Collections.Generic;
using ET;
using UnityEngine;
using UnityEngine.SceneManagement;
using YooAsset;

public static class ResHelp 
{
    public static ETTask<AssetOperationHandle> LoadAssetAsync<T>(string path,string defaultpage="DefaultPackage") where T : UnityEngine.Object
    {
        ETTask<AssetOperationHandle> result = ETTask<AssetOperationHandle>.Create();
        AssetOperationHandle assetOperationHandle =YooAssets.GetAssetsPackage(defaultpage).LoadAssetAsync<T>(path);
        assetOperationHandle.Completed += handle => { result.SetResult(handle); };
        return result;
    }
    
    public static ETTask<SceneOperationHandle> LoadSceneAsync(string scenePath,
        LoadSceneMode loadSceneMode = LoadSceneMode.Single,string defaultpage="DefaultPackage")
    {
        ETTask<SceneOperationHandle> result = ETTask<SceneOperationHandle>.Create();
        SceneOperationHandle sceneOperationHandle =
            YooAssets.GetAssetsPackage(defaultpage).LoadSceneAsync(scenePath, loadSceneMode, true);
        sceneOperationHandle.Completed += handle => { result.SetResult(sceneOperationHandle); };
        return result;
    }
    
    public static ETTask<RawFileOperationHandle> GetRawFileAsync(string path,string defaultpage="DefaultPackage")
    {
        ETTask<RawFileOperationHandle> result = ETTask<RawFileOperationHandle>.Create();
        RawFileOperationHandle rawFileOperationHandle = YooAssets.GetAssetsPackage(defaultpage).LoadRawFileAsync(path);
        rawFileOperationHandle.Completed += handle => { result.SetResult(rawFileOperationHandle); };
        return result;
    }
    
    
    public static List<string> GetAssetPathsByTag(string tag)
    {
        AssetInfo[] assetInfos = YooAssets.GetAssetInfos(tag);
        List<string> result = new List<string>(16);
        foreach (var assetInfo in assetInfos)
        {
            result.Add(assetInfo.Address);
        }

        return result;
    }

    public static void UnLoadUnusedAssets(string defaultpage="DefaultPackage")
    {
        YooAssets.GetAssetsPackage(defaultpage).UnloadUnusedAssets();
    }
}
